<template>
  <el-form ref="addForm" :model="formData" :rules="rules" label-width="80px">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="formData.username" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="手机号" prop="mobile">
      <el-input v-model="formData.mobile" placeholder="请输入手机号" />
    </el-form-item>
    <el-form-item label="入职时间" prop="timeOfEntry">
      <el-date-picker v-model="formData.timeOfEntry" type="date" placeholder="请选择入职日期" style="width: 100%;" />
    </el-form-item>
    <el-form-item label="聘用形式" prop="formOfEmployment">
      <el-select v-model="formData.formOfEmployment" placeholder="请选择">
        <el-option v-for="obj in hireType" :key="obj.id" :label="obj.value" :value="obj.id" />
      </el-select>
    </el-form-item>
    <el-form-item label="工号" prop="workNumber">
      <el-input v-model="formData.workNumber" placeholder="请输入工号" />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <el-input
        v-model="formData.departmentName"
        placeholder="请选择部门"
        @focus="deptNameFocus"
        @blur="deptNameBlur"
      />
      <div v-show="showTree">
        <el-tree
          :data="treeData"
          :props="{'label':'name'}"
          default-expand-all
          @node-click="handleNodeClick"
        />
      </div>
    </el-form-item>
    <el-form-item label="转正时间">
      <el-date-picker v-model="formData.correctionTime" type="date" placeholder="请选择转正日期" style="width: 100%;" />
    </el-form-item>
    <el-form-item style="text-align: center">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="addSubmit">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import EmployeeEnum from '@/constant/employees'
import { getDepartmentsAPI } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import { addEmployeeAPI } from '@/api/employees'

export default {
  name: 'EmpDialog',
  data() {
    return {
      // 添加表单字段
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },

      // 添加表单的校验字段
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      },

      // 聘用形式
      hireType: EmployeeEnum.hireType,

      // 树形结构数据
      treeData: [],

      // 树形结构状态
      showTree: false
    }
  },

  methods: {
    // 点击确定按钮
    addSubmit() {
      this.$refs.addForm.validate(async valid => {
        // 兜底验证
        if (!valid) return

        // 兜底验证通过,调接口
        const res = await addEmployeeAPI(this.formData)
        console.log(res)

        // 反馈用户
        this.$message.success(res.message)

        // 表单清空
        this.$refs.addForm.resetFields()

        // 通知父组件
        this.$emit('update-empolyee')
      })
    },

    // 点击取消按钮
    cancel() {
      // 表单清空
      this.$refs.addForm.resetFields()

      // 通知父组件
      this.$emit('close')
    },

    // 聘用形式输入框聚焦
    deptNameFocus() {
      // 拿数据
      this.getDepartments()
      // 显示控件
      this.showTree = true
    },

    // 聘用形式输入框失去焦点
    deptNameBlur() {
      setTimeout(() => {
        this.showTree = false
      }, 100)
    },

    // 获取组织架构
    async getDepartments() {
      // 调接口,拿数据
      const res = await getDepartmentsAPI()
      // 数据处理成树形结构
      this.treeData = tranListToTreeData(res.data.depts, '')
    },

    // 点击树形结构节点触发,形参data
    handleNodeClick(data) {
      // 需求,节点有子级,不让选中
      if (data && data.children) return

      // 部门赋值
      this.formData.departmentName = data.name

      // 隐藏 tree
      this.showTree = false
    },

    // 父组件传的方法
    resetFields() {
      // 父组件,对话框close事件,触发子组件resetFields方法,resetFields是父组件自定义的
      this.$refs.addForm.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form{
    margin: 0 60px 0 10px;
}
.el-select{
    width: 100%;
}

</style>
